package com.candy.controller;

import com.candy.domain.User;
import com.candy.service.LoginService;
import com.springMVC.annotation.*;

import java.time.Instant;

/**
 * @author ：candy
 * @date ：Created in 2020/11/30 20:25
 * @description：
 * @version: 1.0
 * <p>
 * <p>
 * http://localhost:7098/login
 * http://localhost:7098/loginForward
 * http://localhost:7098/loginRedirect
 * http://localhost:7098/getUser?name=candy
 * <p>
 * <p>
 */
@Controller
public class LoginController {

    @AutoWired("loginService")
    private LoginService loginService;

    // 简单请求页面
    @RequestMapping("/login")
    public String getLogin() {
        return "login.jsp";
    }

    // 简单请求转发
    @RequestMapping("/loginForward")
    public String getLoginForward() {
        return "forward:login.jsp";
    }

    // 简单请求重定向
    @RequestMapping("/loginRedirect")
    public String getLoginRedirect() {
        return "redirect:login.jsp";
    }

    // 请求转json格式
    @RequestMapping("/getUser")
    @ResponseBody
    public User getUser(@RequestParam("name") String name, String value) {
        User user = loginService.getUser();
        System.out.println("===" + name);
        user.setName(name);
        return user;
    }

    // 请求转json格式
    @RequestMapping("/getUserById")
    @ResponseBody
    public User getUserById(Integer id) {
        Instant instant = Instant.now();

        instant.getEpochSecond();
        instant.toEpochMilli();
        if (id.equals(1))
            return loginService.getUser();
        else
            return null;
    }

}
